package com.hfi.anemicVsdomain.ddd;

import lombok.Builder;
import lombok.Data;
import org.joda.money.Money;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * DDD 实体对象 具有丰富的业务行为
 * @author ChangLiang
 * @date 2020/5/18
 */
@Data
@Builder
public class Contract {

    private Long id;

    /**
     * 合同的产品
     */
    private Product product;

    /**
     * 金额
     */
    private Money revenue;

    /**
     * 签订日期
     */
    private Date signedDate;

    /**
     * 应收款
     */
    @Builder.Default
    private List<RevenueRecognition> list = new ArrayList<>();



    public void addRevenueRecognition(RevenueRecognition revenueRecognition) {
        list.add(revenueRecognition);
    }

    /**
     * query contract revenue till asOfDate
     * @param asOfDate
     * @return
     */
    public Money getRecognizedRevenue(Date asOfDate) {
        return Money.total(list.stream()
                .filter(r -> r.getRecognizedDate().before(asOfDate))
                .map(r -> r.getAmount()).collect(Collectors.toList()));
    }

    public void calculateRecognitions(){
        product.calculateRecognitions(this);
    }
}
